<?php

namespace app\jigou\controller\qingdongams\notice;
use app\jigou\controller\qingdongams\Base;
use addons\qingdongams\model\AftermarketType as AftermarketTypeModel;
use think\DB;
use fast\Tree;
use think\Exception;

//信息库 售后问题
class AftermarketType extends Base {

    /**
     * @var \addons\qingdongams\model\AftermarketType
     */
    protected $model = null;
    protected $catelist;

	public function _initialize() {
		parent::_initialize();
		$this->model = new AftermarketTypeModel();
        Tree::instance()->init(collection($this->model->where([])->order('sort desc,id asc')->select())->toArray());
        $this->catelist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'name');
        $catedata = [0 => __('None')];
        foreach ($this->catelist as $k => &$v) {
            $catedata[$v['id']] = $v['name'];
        }
        unset($v);
        $this->view->assign('catedata', $catedata);
	}


    public function index()
    {
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            $total = count($this->catelist);
            $result = array("total" => $total, "rows" => $this->catelist);
            return json($result);
        }
        return $this->view->fetch();
    }

	//添加
	public function add() {
		if ($this->request->isAjax()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                $result = $this->model->allowField(true)->save($params);
                if ($result === false) {
                    $this->error($this->model->getError());
                }
                $this->success();
            }
            $this->error();
		}

		return $this->view->fetch();
	}


	//修改
	public function edit($ids = null) {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            if ($params) {
                $res = $this->model->isUpdate(true,['id'=>$row['id']])->save(['name'=>$params['name'],'sort'=>$params['sort']]);
                if ($res === false) {
                    $this->error("本地修改失败，请再此尝试！");
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
	}


	//删除
	public function del($ids = null) {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        if ($ids) {
            $delIds = explode(',', $ids);
            Db::startTrans();
            try{
                foreach ($delIds as $k => $v) {
                    $delIds1 = $this->model->where('pid',$v)->column("id");// 可能二级
                    if($delIds1){
                        throw new Exception("此分类下还有分类，请先移除分类");
                    }
                }
                $count = $this->model->where('id', 'in', $delIds)->delete();
                if (!$count) {
                    throw new Exception("删除失败");
                }
                Db::commit();
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            $this->success();
        }
        $this->error();
	}
}